package com.atguigu.write;

import com.atguigu.bean.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;

import java.io.IOException;
import java.util.HashMap;

public class Es01_SingleWrite {
    public static void main(String[] args) throws IOException {
        //1.创建客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        //2.设置链接属性
        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //3.获取链接
        JestClient jestClient = jestClientFactory.getObject();

        //4.将数据写入ES
//        Index index = new Index.Builder("{\n" +
//                "  \"id\":\"102\",\n" +
//                "  \"name\":\"分手大师\"\n" +
//                "}")
//                .index("movie")
//                .type("_doc")
//                .id("1002")
//                .build();

        //把数据封装成Map写入ES
//        HashMap<String, String> source = new HashMap<>();
//        source.put("id", "103");
//        source.put("name", "逃学威龙2");
//        Index index = new Index.Builder(source)
//                .index("movie")
//                .type("_doc")
//                .id("1003")
//                .build();
        //把数据封装成JavaBean写入ES
        Movie movie = new Movie("104", "功夫");
        Index index = new Index.Builder(movie)
                .index("movie")
                .type("_doc")
                .id("1004")
                .build();
        jestClient.execute(index);



        //关闭链接
        jestClient.shutdownClient();
    }
}
